<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

/**
 * 发布信息模块
 */
class postForm extends MY_Controller
{

    private $_typeId = array(
        'rent' => 1,
        'rentneed' => 2,
        'buyneed' => 3,
        'buy' => 4,
    );
    
    /**
     * 获取区域数据
     * @return array 
     */
    private function _getZoneData()
    {
        $zoneData = $this->model_common_getZoneData();
        $result = array( );
        foreach ( $zoneData as $single )
            $result[$single['id']] = $single['name'];
        return $result;
    }

    /**
     * 获取室厅数据
     * @return array
     */
    private function _getRoomData()
    {
        $roomData = $this->model_common_getRoomData();
        $result = array( );
        foreach ( $roomData as $single )
            $result[$single['id']] = $single['name'];
        return $result;
    }

    /**
     * 加载出租表单
     * @param array $data
     * @return string
     */
    public function rent( $data = array( ) )
    {
        $data['zoneData'] = $this->_getZoneData();
        $data['roomData'] = $this->_getRoomData();
        return $this->load->view( 'rent_form', $data );
    }
    
    /**
     * 加载出售表单
     * @param array $data
     * @return string
     */
    public function sell( $data = array( ) )
    {
        $data['zoneData'] = $this->_getZoneData();
        $data['roomData'] = $this->_getRoomData();
        return $this->load->view( 'sell_form', $data );
    }

    /**
     * 修改信息
     * @param int $postId 信息ID
     */
    public function rentEdit( $postId )
    {
        $data['zoneData'] = $this->_getZoneData();
        $data['roomData'] = $this->_getRoomData();
        $data['postData'] = $this->model_post_getById( $postId );
        $postImg = $this->model_attachment_getByPostId( $postId );
        if ( $postImg ) $data['postImg'] = $postImg;
        return $this->load->view( 'rentedit_form', $data );
    }
    
    /**
     * 修改信息
     * @param int $postId 信息ID
     */
    public function sellEdit( $postId )
    {
        $data['zoneData'] = $this->_getZoneData();
        $data['roomData'] = $this->_getRoomData();
        $data['postData'] = $this->model_post_getById( $postId );
        $postImg = $this->model_attachment_getByPostId( $postId );
        if ( $postImg ) $data['postImg'] = $postImg;
        return $this->load->view( 'selledit_form', $data );
    }
    
    /**
     * 处理出租信息提交
     * @return array 
     */
    public function rentSubmit()
    {
        if ( !$this->_validation( 'rent' ) ) return false;
        return $this->_getFormData( 'rent' );
    }
    
    /**
     * 处理出租信息提交
     * @return array 
     */
    public function sellSubmit()
    {
        if ( !$this->_validation( 'sell' ) ) return false;
        return $this->_getFormData( 'sell' );
    }

    /**
     * 验证表单数据合法性
     * @param string $formType 表单类型
     * @return bool
     */
    private function _validation( $formType )
    {
        if ( $formType == 'rent' )
        {
            $this->form_validation->set_message( 'required', '%s不能为空' );
            $this->form_validation->set_rules( 'title', '标题', 'required' );
            $this->form_validation->set_rules( 'zone', '所在区域', 'required' );
            $this->form_validation->set_rules( 'road', '所在街道公寓', 'required' );
            $this->form_validation->set_rules( 'room', '户型', 'required' );
            $this->form_validation->set_rules( 'floor', '所在楼层', 'required' );
            $this->form_validation->set_rules( 'amount', '租金', 'required' );
            $this->form_validation->set_rules( 'square', '平方米', 'required' );
            $this->form_validation->set_rules( 'content', '描述', 'required' );
            $this->form_validation->set_rules( 'ispersonal', '个人/中介', 'required' );
        } else if ( $formType == 'sell' )
        {
            $this->form_validation->set_message( 'required', '%s不能为空' );
            $this->form_validation->set_rules( 'title', '标题', 'required' );
            $this->form_validation->set_rules( 'zone', '所在区域', 'required' );
            $this->form_validation->set_rules( 'road', '所在街道公寓', 'required' );
            $this->form_validation->set_rules( 'room', '户型', 'required' );
            $this->form_validation->set_rules( 'floor', '所在楼层', 'required' );
            $this->form_validation->set_rules( 'amount', '金额', 'required' );
            $this->form_validation->set_rules( 'square', '平方米', 'required' );
            $this->form_validation->set_rules( 'content', '描述', 'required' );
            $this->form_validation->set_rules( 'ispersonal', '个人/中介', 'required' );
        }
        return $this->form_validation->run();
    }

    /**
     * 获取提交的表单数据
     * @param array $formType 
     */
    private function _getFormData( $formType )
    {
        $formData = array();
        if ( $formType == 'rent' )
        {
            $formData = array(
                'title' => $this->input->post('title'),
                'zone' => intval( $this->input->post('zone') ),
                'road' => $this->input->post('road'),
                'room' => intval( $this->input->post('room') ),
                'floor' => intval( $this->input->post('floor') ),
                'amount' => floatval( $this->input->post('amount') ),
                'square' => intval( $this->input->post('square') ),
                'content' => $this->input->post('content'),
                'userid' => $this->session->getUserSession('id'),
                'type' => $this->getTypeId('rent'),
                'ispersonal' => $this->input->post('ispersonal'),
            );
        } else if ( $formType == 'sell' )
        {
            $formData = array(
                'title' => $this->input->post('title'),
                'zone' => intval( $this->input->post('zone') ),
                'road' => $this->input->post('road'),
                'room' => intval( $this->input->post('room') ),
                'floor' => intval( $this->input->post('floor') ),
                'amount' => floatval( $this->input->post('amount') ),
                'square' => intval( $this->input->post('square') ),
                'content' => $this->input->post('content'),
                'userid' => $this->session->getUserSession('id'),
                'type' => $this->getTypeId('buy'),
                'ispersonal' => $this->input->post('ispersonal'),
            );
        }
        return $formData;
    }
    
    /**
     * 获取类型ID
     * @param string $key
     * @return int
     */
    public function getTypeId( $key )
    {
        return isset( $this->_typeId[$key] ) ? $this->_typeId[$key] : 1;
    }
}